package com.webtest.controller;

import com.baomidou.mybatisplus.annotation.TableId;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.webtest.dao.BorrowMapper;
import com.webtest.entity.*;
import com.webtest.service.*;
import com.webtest.service.admin.AdminUserService;
import com.webtest.vo.SearchBooks;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class BookBorrowController {
    @Autowired
    private BorrowMapper borrowMapper;
    @Autowired
    private BorrowService borrowService;

    @Autowired
    private ReturnService returnService;

    @Autowired
    private UserService userService;
    @Autowired
    private AdminUserService adminUserService;
    @Autowired
    private ShelfService shelfService;
    @Autowired
    private CommentService commentService;
    @RequestMapping("/borrow")
    public String toMyBorrow(HttpSession session, Model model,@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum){
        User user = (User)session.getAttribute("user");
        User userById = userService.findUserById(user.getId());
        model.addAttribute("userMessage",userById);
        PageHelper.startPage(pageNum,10);
        //根据用户id查所有其所有借阅信息
        List<Book> bookByBorrow = borrowService.findBookByBorrow(user.getId());
        PageInfo<Book> bookPageInfo = new PageInfo<>(bookByBorrow);
        // 获取所有书架
        List<Shelf> allShelf = shelfService.getAllShelf();
        model.addAttribute("allShelf", allShelf);
        model.addAttribute("pageInfo",bookPageInfo);
        return "borrow";
    }

    //搜索
    @PostMapping("/borrow/search")
    public String searchBooks(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                              Model model, SearchBooks searchBooks,HttpSession session){
        // 书名或书号条件搜索
        User user = (User) session.getAttribute("user");
        searchBooks.setUserId(user.getId());
        PageInfo<Book> booklist = borrowService.findBookList(pageNum,10,searchBooks);
        model.addAttribute("pageInfo",booklist);
        return "borrow::bookList";
    }


    @GetMapping("/borrow/{bookId}/{stuId}/return")
    @Transactional
    public String borrowBook(@PathVariable Integer bookId,@PathVariable Integer stuId, RedirectAttributes attributes, HttpSession session) {
//        User user = (User) session.getAttribute("user");
//        System.out.println(bookId);
//        Integer stuId = user.getId();
        //根据用户id查所有其所有借阅信息
//        List<Book> bookByBorrow = borrowService.findBookByBorrow(stuId);

        Borrow borrow = borrowMapper.getById(bookId,stuId,1);
        User userNow = userService.findUserById(stuId);
        if(userNow.getMoney() < borrow.getDamages()){
            attributes.addFlashAttribute("message", "该用户余额不足");
            return "redirect:/admin/borrow";
        }else{
            userNow.setMoney(userNow.getMoney() - borrow.getDamages());
            adminUserService.updateUser(userNow);
        }

        //删除借阅信息
        returnService.deleteBorrow(bookId,stuId);
        returnService.updateReturnBook(bookId);
        attributes.addFlashAttribute("message", "恭喜您，还书成功");
        return "redirect:/admin/borrow";
    }

    @GetMapping("/borrow/{bookId}/return")
    @Transactional
    public String borrowBookAdmin(@PathVariable Integer bookId,RedirectAttributes attributes, HttpSession session) {
//        User user = (User) session.getAttribute("user");
//        System.out.println(bookId);
//        Integer stuId = user.getId();
        //根据用户id查所有其所有借阅信息
//        List<Book> bookByBorrow = borrowService.findBookByBorrow(stuId);
        User user = (User) session.getAttribute("user");
        Integer stuId = user.getId();
        Borrow borrow = borrowMapper.getById(bookId,stuId,1);
        User userNow = userService.findUserById(stuId);
        if(userNow.getMoney() < borrow.getDamages()){
            attributes.addFlashAttribute("message", "该用户余额不足");
            return "redirect:/admin/borrow";
        }else{
            userNow.setMoney(userNow.getMoney() - borrow.getDamages());
            adminUserService.updateUser(userNow);
        }

        //删除借阅信息
        returnService.deleteBorrow(bookId,stuId);
        returnService.updateReturnBook(bookId);
        attributes.addFlashAttribute("message", "恭喜您，还书成功");
        return "redirect:/borrow";
    }


    @GetMapping("/toBorrow/{bookId}")
    @Transactional
    public String toBorrow(@PathVariable Integer bookId,RedirectAttributes attributes, HttpSession session,Model model) {
//        User user = (User) session.getAttribute("user");
//        System.out.println(bookId);
//        Integer stuId = user.getId();
        //根据用户id查所有其所有借阅信息
//        List<Book> bookByBorrow = borrowService.findBookByBorrow(stuId);
        User user = (User) session.getAttribute("user");

        List<Comment> comments = commentService.listComment(bookId);
        model.addAttribute("comments", comments);
        User userById = adminUserService.getUserById(user.getId());
        model.addAttribute("userById",userById);
        model.addAttribute("bookId",bookId);

        // 获取所有书架
        List<Shelf> allShelf = shelfService.getAllShelf();
        model.addAttribute("allShelf", allShelf);

        return "toBorrow";
    }

}
